﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class CGD_Query : BasePage
    {
        CGD_Dao dao = new CGD_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                this.lbTitle.Text = "采购单列表";
                this.tbRQStart.Attributes.Add("onclick", "show()");
                this.tbRQEnd.Attributes.Add("onclick", "show()");
                initValue();
                BindGridview();
            }
        }

        private void initValue()
        {
            // 部门
            Departments_Dao daoDep = new Departments_Dao();
            List<Department> lsDep = daoDep.QueryAll();
            this.ddlBM.DataSource = lsDep;
            this.ddlBM.DataTextField = "Name";
            this.ddlBM.DataValueField = "ID";
            this.ddlBM.DataBind();
            this.ddlBM.Items.Insert(0, new ListItem("--------请选择--------", "0"));

            // 供应商
            ZL_GYS_Dao daoGys = new ZL_GYS_Dao();
            List<ZL_GYS> lsGys = daoGys.QueryAll();
            this.ddlGYS.DataSource = lsGys;
            this.ddlGYS.DataTextField = "MC";
            this.ddlGYS.DataValueField = "ID";
            this.ddlGYS.DataBind();
            this.ddlGYS.Items.Insert(0, new ListItem("--------请选择--------", "0"));
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            GridView1.PageIndex = RestorePageIndex();

            // 部门
            Departments_Dao daoDep = new Departments_Dao();
            List<Department> lsDep = daoDep.QueryAll();

            // 供应商
            ZL_GYS_Dao daoGys = new ZL_GYS_Dao();
            List<ZL_GYS> lsGys = daoGys.QueryAll();

            // 人员
            Users_Dao daoUsers = new Users_Dao();
            List<User> lsUser = daoUsers.QueryAll();

            List<CGD_Ex> listEx = new List<CGD_Ex>();
            List<CGD> list = query(); //  dao.QueryAll();
            if (list == null)
            {
                return;
            }

            CGD_Ex ce = null;
            foreach (CGD c in list)
            {
                ce = new CGD_Ex();
                Department dep = lsDep.Find(delegate(Department p) { return p.Id == c.BMID; });
                ce.BMID = dep.Name;
                ce.RQ = c.RQ.ToString("yyyy-MM-dd HH:mm:ss");
                User u = lsUser.Find(delegate(User p) { return p.Id == c.RYID; });
                ce.RYID = u.Name;
                ZL_GYS gys = lsGys.Find(delegate(ZL_GYS p) { return p.ID == c.GYSID; });
                ce.GYSID = gys != null ? gys.MC : "";
                ce.ZT = c.ZT;
                ce.CJSJ = c.CJSJ.ToString("yyyy-MM-dd HH:mm:ss");
                ce.ID = c.ID.ToString();

                listEx.Add(ce);
            }

            this.GridView1.DataSource = listEx;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CGD_Ex item = (CGD_Ex)e.Row.DataItem;
                Label lbl = ((Label)(e.Row.Cells[9].FindControl("lblZT")));
                if (item.ZT == 0)
                {
                    lbl.Text = "待采购";
                    lbl.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    lbl.ForeColor = System.Drawing.Color.Green;
                    lbl.Text = "已采购";
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected class CGD_Ex
        {
            public string ID { get; set; }
            public string BMID { get; set; }
            public string RYID { get; set; }
            public string GYSID { get; set; }
            public string RQ { get; set; }
            public int ZT { get; set; }
            public string CJSJ { get; set; }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/CGD_Add.aspx");
        }

        protected class UserNames
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            BindGridview();
        }

        protected List<CGD> query()
        {
            string datetimeStart = this.tbRQStart.Text.Trim();
            string datetimeEnd = this.tbRQEnd.Text.Trim();
            if (!datetimeStart.Equals(string.Empty) && !datetimeEnd.Equals(string.Empty))
            {
                int t = DateTime.Parse(datetimeStart).CompareTo(DateTime.Parse(datetimeEnd));
                if (t > 0)
                {
                    lbResult.Text = "结束时间不能小于开始时间";
                    return null;
                }
            }

            string gysId = this.ddlGYS.SelectedValue;
            string bmid = this.ddlBM.SelectedValue;
            string ryid = this.ddlRY.SelectedValue;

            return dao.QueryBySql(gysId, bmid, ryid, datetimeStart, datetimeEnd);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlBM_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlRY.Items.Clear();
            string depId = ddlBM.SelectedValue;
            if (depId != "0")
            {
                Guid guid = Guid.Parse(depId);
                using (var context = new Dao.ErpEntities())
                {
                    var query = from d in context.UserInDepartmentSet
                                join u in context.UserSet
                                on d.UserId equals u.Id
                                where d.DepartmentId == guid
                                select new
                                {
                                    Id = d.UserId,
                                    Name = u.Name
                                };
                    List<UserNames> list = new List<UserNames>();
                    if (query.Count() > 0)
                    {
                        foreach (var q in query)
                        {
                            UserNames un = new UserNames();
                            un.Id = q.Id.ToString();
                            un.Name = q.Name;
                            list.Add(un);
                        }
                    }

                    this.ddlRY.DataSource = list;
                    this.ddlRY.DataTextField = "Name";
                    this.ddlRY.DataValueField = "Id";
                    this.ddlRY.DataBind();
                }
            }
        }
    }
}